package cn.dankal.flyaudio.adapter;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import com.lihang.ShadowLayout;

import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewAdapter;
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewHolder;
import cn.dankal.basiclib.pojo.CouponEntity;
import cn.dankal.basiclib.util.ActivityUtils;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.friend.ChatShareActivity;
import cn.dankal.flyaudio.personal.CardQrCodeActivity;
import cn.dankal.flyaudio.personal.MyCardDetailActivity;

public class CouponAdapter extends BaseRecyclerViewAdapter<CouponEntity> {
    @Override
    protected int getLayoutResId(int viewType) {
        return R.layout.item_my_card;
    }

    @Override
    protected BaseRecyclerViewHolder getViewHolder(ViewGroup parent, View rootView, int viewType) {
        return new ViewHolder(rootView);
    }

    public class ViewHolder extends BaseRecyclerViewHolder<CouponEntity> {

        public ViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void onBindData(CouponEntity data, int position) {
            ShadowLayout llPrompt = itemView.findViewById(R.id.ll_prompt);
            setText(R.id.tv_coupon_name, data.getName());
            String createTime = "";
            if(data.getPublishedDatetime()!=null) {
                if (data.getPublishedDatetime().length() > 10) {
                    createTime = data.getPublishedDatetime().substring(0, 10);
                }
            }
            String endTime = "";
            if(data.getExpireDatetime()!=null) {
                if (data.getExpireDatetime().length() > 10) {
                    endTime = data.getExpireDatetime().substring(0, 10);
                }
            }
            setText(R.id.tv_time, createTime + "~" + endTime);
//            LinearLayout llUse = itemView.findViewById(R.id.ll_use);
            setGone(R.id.tv_give_away, !data.getCanTransfer());
            switch (data.getState()) {
                //USABLE
                case "USABLE":
                    setImageResId(R.id.iv_bg, R.mipmap.pic_coupon_normal);
                    setGone(R.id.ll_use, false);
                    setTextViewColor(R.id.tv_time, R.color.colorFF);
                    setTextViewColor(R.id.tv_coupon_name, R.color.colorFF);
                    setImageResId(R.id.iv_down, R.mipmap.ic_down_fff);
                    break;
                case "USED":
                    setImageResId(R.id.iv_bg, R.mipmap.pic_coupon_use);
                    setGone(R.id.ll_use, true);
                    setTextViewColor(R.id.tv_time, R.color.color99);
                    setTextViewColor(R.id.tv_coupon_name, R.color.color99);
                    setImageResId(R.id.iv_down, R.mipmap.ic_down_9b9b9b);
                    break;
                case "EXPIRED":
                    setImageResId(R.id.iv_bg, R.mipmap.pic_coupon_invaild);
                    setGone(R.id.ll_use, true);
                    setTextViewColor(R.id.tv_time, R.color.color99);
                    setTextViewColor(R.id.tv_coupon_name, R.color.color99);
                    setImageResId(R.id.iv_down, R.mipmap.ic_down_9b9b9b);
                    break;
                case "TRANSFERRED":
                    setImageResId(R.id.iv_bg, R.mipmap.pic_coupon_gift_end);
                    setGone(R.id.ll_use, true);
                    setTextViewColor(R.id.tv_time, R.color.colorFF);
                    setTextViewColor(R.id.tv_coupon_name, R.color.colorFF);
                    setImageResId(R.id.iv_down, R.mipmap.ic_down_fff);
                    break;
                case "TRANSFERRING":
                    setImageResId(R.id.iv_bg, R.mipmap.pic_coupon_gift);
                    setGone(R.id.ll_use, true);
                    setTextViewColor(R.id.tv_time, R.color.colorFF);
                    setTextViewColor(R.id.tv_coupon_name, R.color.colorFF);
                    setImageResId(R.id.iv_down, R.mipmap.ic_down_fff);
                    break;
            }
            itemView.findViewById(R.id.iv_down).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (llPrompt.getVisibility() == View.GONE) {
                        itemView.findViewById(R.id.ll_prompt).setVisibility(View.VISIBLE);
                    } else {
                        itemView.findViewById(R.id.ll_prompt).setVisibility(View.GONE);
                    }
                }
            });


            itemView.findViewById(R.id.tv_give_away).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //TODO 赠送
                    Intent intent = new Intent();
                    intent.setClass(context, ChatShareActivity.class);
                    intent.putExtra("couponId", data.getUuid());
                    intent.putExtra("couponName",data.getName());
                    ActivityUtils.startActivity(intent);
                }
            });
            itemView.findViewById(R.id.tv_use).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String createTime = "";
                    if (data.getPublishedDatetime().length() > 10) {
                        createTime = data.getPublishedDatetime().substring(0, 10);
                    }
                    String endTime = "";
                    if (data.getExpireDatetime().length() > 10) {
                        endTime = data.getExpireDatetime().substring(0, 10);
                    }
                    Bundle bundle = new Bundle();
                    bundle.putString("time", createTime + "~" + endTime);
                    bundle.putString("code", data.getUuid());
                    bundle.putString("name", data.getName());
                    ActivityUtils.startActivity(bundle, CardQrCodeActivity.class);
                }
            });
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", data.getUuid());
                    ActivityUtils.startActivity(bundle, MyCardDetailActivity.class);
                }
            });
        }
    }
}
